/*
 * CreateComment.java
 *
 * Created on 20 novembre 2007, 16:20
 */
package com.reimscopie.console ;

import java.io.* ;
import java.util.* ;

import javax.naming.* ;
import javax.transaction.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.interfaces.* ;


/**
 * This command creates a new comment for a product.
 */
public class CreateComment extends BeanCommand {
  
  /**
   * Create a new <code>CreateComment</code> instance.
   */
  public CreateComment() {
    
    super("com.reimscopie.beans.session.ReimsCopieAdminBean",
        ReimsCopieAdminRemote.class) ;
    
  }
  
  
  /**
   * Return the name of this command. The name is the first word in the shell
   * command line.
   *
   * @return The command name.
   */
  public String getName() {
    return "create-comment" ;
  }
  
  
  /**
   * Return a short help message about this command.
   *
   * @return The command help.
   */
  public String getHelp() {
    return "Create a new comment about a product" ;
  }
  
  
  /**
   * Return the command usage. This string must give all command options with
   * a description for each option.
   *
   * @return The command usage.
   */
  public String getUsage() {
    
    StringBuilder builder = new StringBuilder() ;
        
    builder.append("create-comment -product-id id -comment c -login l\n") ;
    builder.append("  product-id  The ID of the product\n") ;
    builder.append("  comment     The comment to post\n") ;
    builder.append("  login       The login of the poster account\n") ;
    
    return builder.toString() ;
  }
  
  
  /**
   * Execute this command.
   *
   * @param args The arguments given in the command line.
   * @param output The command output.
   *
   * @exception CommandException Arguments are invalid.
   */
  public void execute(String[] args,ConsoleOutput output)
  throws CommandException {
    
    int productId  = requireIntegerOption("product-id",args) ;
    
    String comment = requireOption("comment",args) ;
    String login   = requireOption("login", args) ;
    
    doCreate(productId,comment,login,output) ;
    
  }
  
  
  /**
   * Create the comment.
   *
   * @param productId The product identifier, where to add the comment.
   * @param comment The comment to add.
   * @param login The poster login.
   * @param output The command output.
   */
  private void doCreate(int productId,String comment,String login,
      ConsoleOutput output) {
    
    try {
      // get JNDI initial context
      InitialContext context      = new InitialContext() ;
      ReimsCopieAdminRemote bean  = (ReimsCopieAdminRemote)getBean(context) ;
      UserTransaction transaction = getTransaction(context) ;
      
      if (bean == null) {
        output.printError("comment not created (no bean)",true) ;
        return ;
      }
      
      try {
        transaction.begin() ;
        Account account = bean.getAccount(login) ;
        if (account == null) {
          output.printError("account not found",true) ;
          transaction.rollback() ;
          return ;
        }
        
        Comment com = new Comment(comment,new Date(),account) ;
        bean.createComment(com,productId) ;
        transaction.commit() ;
        output.print("Done",true) ;

      }
      catch (Exception e) {
        output.printError("Creation failed",true) ;
        try {
          transaction.rollback() ;
        }
        catch (SystemException ex) {
          output.printError(ex) ;
        }
        output.printError(e) ;
      }
      
    }
    catch (NamingException e) {
      output.printError("Cannot get initial context for JNDI") ;
      output.printError(e) ;
      return ;
    }
  }
  
}
